﻿using Mes.原料管理;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mes.仓库管理.WareHouse
{
    public partial class WareHouseEdit: Form
    {
        private string _materialId;

        public WareHouseEdit(string materialId)
        {
            InitializeComponent();
            _materialId = materialId;
            LoadMaterialData();
        }
        private async void LoadMaterialData()
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://10.223.8.76:8085/");
                    HttpResponseMessage response = await client.GetAsync($"/api/WareHouse/EditWareHouse?Id={_materialId}");

                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        if (string.IsNullOrEmpty(json))
                        {
                            MessageBox.Show("API 返回的数据为空！");
                            return;
                        }
                        var apiResponse = JsonConvert.DeserializeObject<ApiResponseEuid>(json);
                        // 检查 material 是否为
                        if (apiResponse?.Code != 200 || apiResponse.Data == null || apiResponse.Data.Count == 0)
                        {
                            MessageBox.Show("API 返回的数据无效！");
                            return;
                        }
                        var material = apiResponse.Data[0];
                        txtWarehouseNoedit.Text = material.WarehouseNo.ToString();
                        txtWarehouseNameedit.Text = material.WarehouseName.ToString();
                        txtAreaedit.Text = material.Area.ToString();
                        selectWarehouseAreaedit.Text = material.WarehouseArea.ToString();
                        txtAddrNoedit.Text = material.AddrNo.ToString();
                        txtHeadPeopleedit.Text = material.HeadPeople.ToString();
                        selectStates.Text = material.States.ToString();
                        txtRemarkedit.Text = material.Remark.ToString();
                        txtPlanedit.Text = material.Plan.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载数据失败：{ex.Message}");
            }
        }

        private async void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                bool statesValue;
                if (!bool.TryParse(selectStates.Text, out statesValue))
                {
                    MessageBox.Show("States 字段的值无效，请选择有效的布尔值。");
                    return;
                }

                var material = new
                {
                    Id = _materialId,
                    WarehouseNo = txtWarehouseNoedit.Text,
                    WarehouseName = txtWarehouseNameedit.Text,
                    Area = txtAreaedit.Text,
                    WarehouseArea = selectWarehouseAreaedit.Text,
                    AddrNo = txtAddrNoedit.Text,
                    HeadPeople = txtHeadPeopleedit.Text,
                    States = statesValue, // 确保 States 是布尔值
                    Remark = txtRemarkedit.Text,
                    Plan = txtPlanedit.Text,
                };

                string json = JsonConvert.SerializeObject(material);
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://10.223.8.76:8085");
                    HttpResponseMessage response = await client.PostAsync("api/WareHouse/UpdateWareHouse", content);

                    if (response.IsSuccessStatusCode)
                    {
                        MessageBox.Show("修改成功！");
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        string error = await response.Content.ReadAsStringAsync();
                        MessageBox.Show($"修改失败: {response.StatusCode}\n{error}");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生异常: {ex.Message}");
            }
        }
    }
}
